package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.common.Rust;
import com.example.entity.ProductList;
import com.example.entity.User;
import com.example.service.ProductListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/productList")
public class ProductListController {

    @Autowired
    private ProductListService productListService;

    /**
     * 查询所有的数据
     * @return
     */
    @GetMapping("/selectProductList")
    Rust<List<ProductList>> selectProductList(){
        log.info("查询所有的产品数据");
        List<ProductList> list = productListService.list();
        log.info("查询到的数据为: " + list.toString());
        return Rust.success(list);
    }

    /**
     * 查询单个数据
     * @param id
     * @return
     */
    @GetMapping("/selectProduct")
    Rust<ProductList> selectProduct(@PathParam("id") Long id){
        ProductList productList = productListService.getById(id);
        log.info("查询单个产品数据为：" + productList.toString());
        return Rust.success(productList);
    }

    /**
     * 添加数据
     * @param productList
     * @return
     */
    @PostMapping("/save")
    Rust<String> save(@RequestBody ProductList productList){
        log.info("添加产品数据：" + productList.toString());
        productListService.save(productList);
        return Rust.success("添加成功");
    }

    /**
     * 删除单个数据
     * @param id
     * @return
     */
    @DeleteMapping("/removeById")
    Rust<String> removeById(@PathParam("id") Long id){
        log.info("删除的数据id值为：" + id);
        productListService.removeById(id);
        return Rust.success("删除成功");
    }

    /**
     * 批量删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/removeByIds")
    Rust<String> removeByIds(@RequestBody Long[] ids){
        log.info("批量删除的id值为：" + Arrays.toString(ids));
        for(Long id : ids){
            productListService.removeById(id);
        }
        return Rust.success("批量删除成功");
    }

    /**
     * 修改数据
     * @param productList
     * @return
     */
    @PutMapping("/updateProduct")
    Rust<String> updateProduct(@RequestBody ProductList productList){
        log.info("要修改的数据为：" + productList.toString());
        productListService.updateById(productList);
        return Rust.success("修改成功");
    }
}
